package com.example.userclient;

import android.os.Parcel;
import android.os.Parcelable;

public class MvcResult<T extends Parcelable> implements Parcelable {

    private static final String  EMPTY = "empty";
    private int code;
    private String message;
    private T data;


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.code);
        dest.writeString(this.message);
        if(this.data==null){
            dest.writeString(EMPTY);
        }else {
            dest.writeString(this.data.getClass().getName());
        }

        dest.writeParcelable(this.data, flags);
    }


    public MvcResult() {
    }

    protected MvcResult(Parcel in) throws ClassNotFoundException {
        this.code = in.readInt();
        this.message = in.readString();
        String className = in.readString();
        if (className.equals(EMPTY)){
            this.data = null;
        }else {
            this.data = in.readParcelable(Class.forName(className).getClassLoader());
        }

    }

    public static final Creator<MvcResult> CREATOR = new Creator<MvcResult>() {
        @Override
        public com.example.userclient.MvcResult createFromParcel(Parcel source) {
            try {
                return new com.example.userclient.MvcResult(source);
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
                return null;
            }
        }

        @Override
        public com.example.userclient.MvcResult[] newArray(int size) {
            return new com.example.userclient.MvcResult[size];
        }
    };
}
